home *** CD-ROM | disk | FTP | other *** search
- ' Caption: Find/Replace RegExpr...|
- ' Hint: Find and Replace with Regular Expression|
- ' Icon: regexpfind.ico|
- '
- ' syn
- ' Copyright (C) 2000-2003, Ascher Stefan. All rights reserved.
- ' stievie@utanet.at, http://web.utanet.at/ascherst/
- '
- ' The contents of this file are subject to the Mozilla Public License
- ' Version 1.1 (the "License"); you may not use this file except in compliance
- ' with the License. You may obtain a copy of the License at
- ' http://www.mozilla.org/MPL/
- '
- ' Software distributed under the License is distributed on an "AS IS" basis,
- ' WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
- ' the specific language governing rights and limitations under the License.
- '
- ' The Original Code is regexpfind.vbs, released Sun, 26 May 2002 10:55:39 UTC.
- '
- ' The Initial Developer of the Original Code is Ascher Stefan.
- ' Portions created by Ascher Stefan are Copyright (C) 2000-2003 Ascher Stefan.
- ' All Rights Reserved.
- '
- ' Contributor(s): .
- '
- ' Alternatively, the contents of this file may be used under the terms of the
- ' GNU General Public License Version 2 or later (the "GPL"), in which case
- ' the provisions of the GPL are applicable instead of those above.
- ' If you wish to allow use of your version of this file only under the terms
- ' of the GPL and not to allow others to use your version of this file
- ' under the MPL, indicate your decision by deleting the provisions above and
- ' replace them with the notice and other provisions required by the GPL.
- ' If you do not delete the provisions above, a recipient may use your version
- ' of this file under either the MPL or the GPL.
- '
- ' You may retrieve the latest version of this file at the syn home page,
- ' located at http://syn.sourceforge.net/
- '
- ' $Id: regexpfind.vbs,v 1.4.2.5 2003/08/13 00:38:45 neum Exp $
-
-
- ' Find and replace Text in the Active Document using Regular Expressions
-
- option explicit
-
- dim FindDialog
- dim FindWhatEdit
- dim ReplaceWithEdit
- dim FindButton, ReplaceButton
- dim CancelButton
- dim chkIgnoreCase
- dim rgScope, rgDirection
- dim RegExpr
- dim MIndex
- dim RMatches
- dim SelBegin
- const RegKey = "HKCU\Software\Ascher\syn\Macros"
-
- '#include <cmnfunc.vbs>
- '#include <consts.vbs>
-
- sub FindEditChange(Sender)
- FindButton.Enabled = Len(FindWhatEdit.Text) > 0
- ReplaceButton.Enabled = Len(FindWhatEdit.Text) > 0
- end sub
-
- sub SelectMatch(start, length)
- start = start + SelBegin
- with ActiveDocument
- .SelStart = start
- .SelEnd = start + length
- end with
- end sub
-
- sub Match()
- dim InString
- AddItem(FindWhatEdit)
- RegExpr.Pattern = FindWhatEdit.Text
- RegExpr.IgnoreCase = chkIgnoreCase.Checked
- RegExpr.Global = true
- if rgScope.ItemIndex = 0 then
- InString = ActiveDocument.Text
- SelBegin = 0
- else
- InString = ActiveDocument.SelText
- SelBegin = Activedocument.SelStart - 1
- end if
- set RMatches = RegExpr.Execute(InString)
- end sub
-
- sub FindClick(Sender)
- dim RetStr
- dim pos1, pos2
- if RegExpr.Pattern <> FindWhatEdit.Text then
- Match
- if rgDirection.ItemIndex = 0 then
- MIndex = 0
- else
- MIndex = RMatches.Count - 1
- end if
- else
- if rgDirection.ItemIndex = 0 then
- MIndex = MIndex + 1
- else
- MIndex = MIndex - 1
- end if
- end if
-
- if RMatches.Count > 0 then
- if rgDirection.ItemIndex = 0 then
- if MIndex < RMatches.Count then
- pos1 = RMatches(MIndex).FirstIndex
- pos2 = Len(RMatches(MIndex).Value)
- SelectMatch pos1, pos2
- else
- MsgBox "No more Matches found.", vbInformation
- end if
- else
- if MIndex => 0 then ' 0-based
- pos1 = RMatches(MIndex).FirstIndex
- pos2 = Len(RMatches(MIndex).Value)
- SelectMatch pos1, pos2
- else
- MsgBox "No more Matches found.", vbInformation
- end if
- end if
- else
- MsgBox "No Matches found.", vbInformation
- end if
- end sub
-
- sub ReplaceClick(Sender)
- AddItem(ReplaceWithEdit)
- if RegExpr.Test(ActiveDocument.SelText) then
- ActiveDocument.SelText = ReplaceWithEdit.Text
- Match
- MIndex = MIndex - 1
- end if
- FindClick(Sender)
- end sub
-
- sub AddItem(Combo)
- dim tmp
- dim ix
- const MaxHis = 10 ' Change this value to have more or less Items in the ComboBox List
- if Combo.Text = "" then exit sub
-
- tmp = Combo.Text
- ix = Combo.Items.IndexOf(Combo.Text)
- if ix > -1 then
- Combo.Items.Move ix, 0
- else
- Combo.Items.Insert 0, Combo.Text
- end if
- if Combo.Items.Count > MaxHis then
- Combo.Items.Delete MaxHis - 1
- end if
- Combo.Text = tmp
- end sub
-
- sub FormShow(Sender)
- ' Load Settings
- FindWhatEdit.Items.Text = RegGetSettings(AddBackslash(RegKey) & "re_searchhis", "")
- ReplaceWithEdit.Items.Text = RegGetSettings(AddBackslash(RegKey) & "re_replacehis", "")
- chkIgnoreCase.Checked = RegGetSettings(AddBackslash(RegKey) & "re_ignorecase", false)
- rgScope.ItemIndex = RegGetSettings(AddBackslash(RegKey) & "re_scope", 0)
- rgDirection.ItemIndex = RegGetSettings(AddBackslash(RegKey) & "re_dir", 0)
- if ActiveDocument.BlockBeginY = ActiveDocument.BlockEndY then
- FindWhatEdit.Text = ActiveDocument.WordAtCaret
- end if
- FindEditChange null
- end sub
-
- sub FormDestroy(Sender)
- ' Save Settings
- if FindWhatEdit.Items.Count > 0 then
- RegSetSettings AddBackslash(RegKey) & "re_searchhis", FindWhatEdit.Items.Text
- end if
- if ReplaceWithEdit.Items.Count > 0 then
- RegSetSettings AddBackslash(RegKey) & "re_replacehis", ReplaceWithEdit.Items.Text
- end if
- RegSetSettings AddBackslash(RegKey) & "re_ignorecase", chkIgnoreCase.Checked
- RegSetSettings AddBackslash(RegKey) & "re_scope", rgScope.ItemIndex
- RegSetSettings AddBackslash(RegKey) & "re_dir", rgDirection.ItemIndex
- end sub
-
- sub Main(dummy)
- dim Label1, Label2
- if Documents.Count = 0 then
- MsgBox "No Document open. Open a Document and test this excellent Script!!!!!", vbCritical
- exit sub
- end if
-
- FindDialog = Create("TForm", Self, "FindForm")
- with FindDialog
- .Caption = "Find/Replace using Regular Expression"
- .Position = "poOwnerFormCenter"
- .BorderStyle = "bsDialog"
- .Width = 370
- .Height = 180
- .OnShow = "FormShow"
- .OnDestroy = "FormDestroy"
- end with
-
- Label1 = Create("TLabel", FindDialog)
- with Label1
- .Parent = FindDialog
- .Caption = "&Find what:"
- .Left = 8
- .Top = 12
- .FocusControl = FindWhatEdit
- end with
- FindWhatEdit = Create("TComboBox", FindDialog)
- with FindWhatEdit
- .Parent = FindDialog
- .Left = 80
- .Top = 8
- .Width = 190
- .Height = 21
- .OnChange = "FindEditChange"
- end with
- Label2 = Create("TLabel", FindDialog)
- with Label2
- .Parent = FindDialog
- .Caption = "&Replace with:"
- .Left = 8
- .Top = 35
- .FocusControl = ReplaceWithEdit
- end with
- ReplaceWithEdit = Create("TComboBox", FindDialog)
- with ReplaceWithEdit
- .Parent = FindDialog
- .Left = 80
- .Top = 33
- .Width = 190
- .Height = 21
- end with
-
- FindButton = Create("TButton", FindDialog)
- with FindButton
- .Parent = FindDialog
- .Caption = "F&ind"
- .Default = true
- .Left = 280
- .Top = 7
- .Width = 75
- .Enabled = false
- .Hint = "Find Match"
- .OnClick = "FindClick"
- end with
- ReplaceButton = Create("TButton", FindDialog)
- with ReplaceButton
- .Parent = FindDialog
- .Caption = "R&eplace"
- .Left = 280
- .Top = 40
- .Width = 75
- .Enabled = false
- .Hint = "Replace Match"
- .OnClick = "ReplaceClick"
- end with
- CancelButton = Create("TButton", FindDialog)
- with CancelButton
- .Parent = FindDialog
- .Caption = "&Close"
- .Cancel = true
- .ModalResult = mrCancel
- .Left = 280
- .Top = 120
- .Width = 75
- .Hint = "Close this Dialog"
- end with
-
- chkIgnoreCase = Create("TCheckBox", FindDialog)
- with chkIgnoreCase
- .Parent = FindDialog
- .Caption = "&Ignore Case"
- .Left = 8
- .Top = 68
- end with
- rgScope = Create("TRadioGroup", FindDialog)
- with rgScope
- .Parent = FindDialog
- .Caption = "Scope"
- .Left = 8
- .Top = 90
- .Height = 55
- .Width = 130
- .Items.Add("&Entire Document")
- .Items.Add("&Selection only")
- .ItemIndex = 0
- end with
- rgDirection = Create("TRadioGroup", FindDialog)
- with rgDirection
- .Parent = FindDialog
- .Caption = "Direction"
- .Left = 150
- .Top = 90
- .Height = 55
- .Width = 120
- .Items.Add("F&orward")
- .Items.Add("&Backward")
- .ItemIndex = 0
- end with
-
- set RegExpr = new RegExp
-
- FindDialog.ShowModal
- set RegExpr = nothing
- FindDialog.Free
- end sub
-
-